package com.ruoyi.web.controller.info;

import java.util.Arrays;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.ruoyi.common.core.domain.entity.SysUser;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.info.domain.InfoStu;
import com.ruoyi.info.service.IInfoStuService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 学生Controller
 * 
 * @author ruoyi
 * @date 2024-09-23
 */
@RestController
@RequestMapping("/info/stu")
public class InfoStuController extends BaseController
{
    @Autowired
    private IInfoStuService infoStuService;


    @PreAuthorize("@ss.hasPermi('info:stu:import')")
    @PostMapping("/importData")
    public AjaxResult importData(MultipartFile file, boolean updateSupport) throws Exception
    {
        ExcelUtil<InfoStu> util = new ExcelUtil<InfoStu>(InfoStu.class);
        List<InfoStu> userList = util.importExcel(file.getInputStream());
        String operName = getUsername();
        String message = infoStuService.importStu(userList, updateSupport, operName);
        return success(message);
    }


    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response)
    {
        ExcelUtil<InfoStu> util = new ExcelUtil<InfoStu>(InfoStu.class);
        util.importTemplateExcel(response, "学生数据");
    }



    /**
     * 查询学生列表
     */
    @PreAuthorize("@ss.hasPermi('info:stu:list')")
    @GetMapping("/list")
    public TableDataInfo list(InfoStu infoStu)
    {
        startPage();
        List<InfoStu> list = infoStuService.queryList(infoStu);
        return getDataTable(list);
    }

    /**
     * 导出学生列表
     */
    @PreAuthorize("@ss.hasPermi('info:stu:export')")
    @Log(title = "学生", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, InfoStu infoStu)
    {
        List<InfoStu> list = infoStuService.queryList(infoStu);
        ExcelUtil<InfoStu> util = new ExcelUtil<InfoStu>(InfoStu.class);
        util.exportExcel(response, list, "学生数据");
    }

    /**
     * 获取学生详细信息
     */
    @PreAuthorize("@ss.hasPermi('info:stu:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id)
    {
        return AjaxResult.success(infoStuService.getById(id));
    }

    /**
     * 新增学生
     */
    @PreAuthorize("@ss.hasPermi('info:stu:add')")
    @Log(title = "学生", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody InfoStu infoStu)
    {
        return toAjax(infoStuService.save(infoStu));
    }

    /**
     * 修改学生
     */
    @PreAuthorize("@ss.hasPermi('info:stu:edit')")
    @Log(title = "学生", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody InfoStu infoStu)
    {
        return toAjax(infoStuService.updateById(infoStu));
    }

    /**
     * 删除学生
     */
    @PreAuthorize("@ss.hasPermi('info:stu:remove')")
    @Log(title = "学生", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable Long[] ids)
    {
        return toAjax(infoStuService.removeByIds(Arrays.asList(ids)));
    }
}
